package wassalt.client;

import java.util.ArrayList;
import java.util.Locale;

import wassalt.Utils.AppConstants;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class ListAdapter extends CursorAdapter{
	   	
		private int tab_id;
		TextView basic1;
		TextView basic2;
		TextView info1;
		TextView info2;
		TextView info3;
		TextView info4;
		Context c;
		boolean extended;
		public long cur_sel;
		View cur_sel_view;
		
		//Cursor cur;
		
		ArrayList<Boolean> stat = new ArrayList<Boolean>();
		
		/**
		 * 
		 * @param pos
		 * @return
		 */
		public boolean getExpanded(int pos)
		{
			return stat.get(pos).booleanValue();
		}
		
		/**
		 * 
		 * @param pos
		 * @param val
		 */
		public void setExpanded(int pos, boolean val)
		{
			stat.set(pos, new Boolean(val));
		}
		
		/**
		 * @author Aya Kamel
		 * Constructor
		 */
		public ListAdapter(Context c, Cursor cur, int tab) {
			super(c, cur);
			this.c = c;
			tab_id = tab;
			cur_sel = -1;
		}

		/**
		 * @author Aya Kamel
		 * @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
		 * Initiates the TextViews to be shown in the list 
		 */
		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			
			// TODO Auto-generated method stub
			basic1 = (TextView)view.findViewById(R.id.basic1);
            basic2= (TextView)view.findViewById(R.id.basic2);
            info1 = (TextView)view.findViewById(R.id.info1);
            info2 = (TextView)view.findViewById(R.id.info2);
        	info3 = (TextView)view.findViewById(R.id.info3);
            info4 = (TextView)view.findViewById(R.id.info4);

            stat.add(new Boolean(false));
            switch(tab_id)
            {
	            case AppConstants.TAB_INCOMING:
	            	setIncomingContent(cursor);
	            	extended = true;
	            	break;
	            case AppConstants.TAB_OUTGOING:
	            	setOutgoingContent(cursor);
	            	extended = true;
	            	break;
	            case AppConstants.TAB_SELF:
	            	setSelfContent(cursor);
	            	extended = false;
	            	break;
	            case AppConstants.FAVORITES:
	            	setFavoritesContent(cursor);
	            	extended = false;
	            	break;
            }
		}
		
		/**
		 * Sets the view components in case of favorite notifications tab
		 * @param cursor
		 * 		The cursor containing data
		 * @author Aya Kamel
		 */
		private void setFavoritesContent(Cursor cursor) {
			String before = cursor.getString(cursor.getColumnIndex(AppConstants.VALUE)) + " ";
			String location = cursor.getString(cursor.getColumnIndex(AppConstants.LOCATION));
        	String type = cursor.getString(cursor.getColumnIndex(AppConstants.TYPE));
        	if(type.equals(AppConstants.TIME))
        		before+= " "+c.getString(R.string.minutes);
			else
				before+= " "+c.getString(R.string.meters);
        	
        	basic1.setText(cursor.getString(cursor.getColumnIndex(AppConstants.CNAME)));
			int tab = cursor.getInt(cursor.getColumnIndex(AppConstants.NOT_TYPE));
			
			switch(tab)
			{
			case AppConstants.TAB_INCOMING: 
				basic2.setText(c.getString(R.string.favorites_request));
				break;
			case AppConstants.TAB_OUTGOING:
				basic2.setText(c.getString(R.string.favorites_offer));
				break;
			default: basic2.setText(c.getString(R.string.favorites_self_alarm));
				break;
			}
			
	        info1.setText(location);
		    info2.setText(before);
		    info3.setVisibility(View.GONE);
		    info4.setVisibility(View.GONE);
			
		}

		/**
		 * Sets the view components in the case of the self alarms tab
		 * @param cursor
		 * 		The cursor containing data
		 * @author Aya Kamel
		 */
		private void setSelfContent(Cursor cursor) {
			String before = cursor.getString(cursor.getColumnIndex(AppConstants.VALUE)) + " ";
			String location = cursor.getString(cursor.getColumnIndex(AppConstants.LOCATION));
        	String type = cursor.getString(cursor.getColumnIndex(AppConstants.TYPE));
        	if(type.equals(AppConstants.TIME))
        		before+= " "+c.getString(R.string.minutes);
        	else
				before+= " "+c.getString(R.string.meters);
        	basic1.setText(location);
			basic2.setText(before);
	        if(info1 != null)
	        {
	        	if (Locale.getDefault().getLanguage().equals("ar"))
	        	{
	        		info3.setText(cursor.getString(cursor.getColumnIndex(AppConstants.START))
	        				+": "+c.getString(R.string.starts));
		        	info4.setText(cursor.getString(cursor.getColumnIndex(AppConstants.END))
		        			+": "+c.getString(R.string.ends));
	        	}
	        	else
	        	{
	        		info3.setText(c.getString(R.string.starts)+": "+
		        			cursor.getString(cursor.getColumnIndex(AppConstants.START)));
		        	info4.setText(c.getString(R.string.ends)+": "+
		        			cursor.getString(cursor.getColumnIndex(AppConstants.END)));
	        	}
		        info1.setVisibility(View.GONE);
		        info2.setVisibility(View.GONE);
	        }
			
		}
		
		/**
		 * Gets the localized string of status
		 * @param stat
		 * 		The input status
		 * @return
		 * 		The localized string
		 * @author Aya Kamel
		 */
		private String getStatus(String stat)
		{
			String res = "";
			String[] stati = c.getResources().getStringArray(R.array.received_permission);
			if(stat.equals(AppConstants.ACCEPTED))
			{
				res = stati[0];
			}
			if(stat.equals(AppConstants.REJECTED))
			{
				res = stati[1];
			}
			if(stat.equals(AppConstants.PENDING))
			{
				res = stati[2];
			}
			return res;
		}

		/**
		 * Sets the view components in the case of the outgoing notifications tab
		 * @param cursor
		 * 		The cursor containing data
		 * @author Aya Kamel
		 */
		private void setOutgoingContent(Cursor cursor) {
			
			basic1.setText(cursor.getString(cursor.getColumnIndex(AppConstants.CNAME)));
			boolean requested = cursor.getString(cursor.getColumnIndex(AppConstants.NOT_TYPE)).equals("1");
			
			if(requested)
				basic2.setText(getStatus(cursor.getString(cursor.getColumnIndex(AppConstants.STATUS))));
			else
				basic2.setText(c.getString(R.string.offer_button));
			
			if(info1!=null)
	        {
				String before = cursor.getString(cursor.getColumnIndex(AppConstants.VALUE)) + " ";
				String location = cursor.getString(cursor.getColumnIndex(AppConstants.LOCATION));
	        	String type = cursor.getString(cursor.getColumnIndex(AppConstants.TYPE));
	        	if(type.equals(AppConstants.TIME))
	        		before+= " "+c.getString(R.string.minutes);
				else
					before+= " "+c.getString(R.string.meters);
	        	
	        	info1.setText(location);
	        	info2.setText(before);
	        	
	        	if (Locale.getDefault().getLanguage().equals("ar"))
	        	{
	        		info3.setText(cursor.getString(cursor.getColumnIndex(AppConstants.START))
	        				+": "+c.getString(R.string.starts));
		        	info4.setText(cursor.getString(cursor.getColumnIndex(AppConstants.END))
		        			+": "+c.getString(R.string.ends));
	        	}
	        	else
	        	{
	        		info3.setText(c.getString(R.string.starts)+": "+
		        			cursor.getString(cursor.getColumnIndex(AppConstants.START)));
		        	info4.setText(c.getString(R.string.ends)+": "+
		        			cursor.getString(cursor.getColumnIndex(AppConstants.END)));
	        	}
	        		
	        	
	        	
	        	
	        	info3.setVisibility(View.VISIBLE);
	        	info4.setVisibility(View.VISIBLE);
	        }
			
			
		}

		/**
		 * Sets the view components in the case of the incoming notifications tab
		 * @param cursor
		 * 		The cursor containing data
		 * @author Aya Kamel
		 */
		private void setIncomingContent(Cursor cursor) {
			
			basic1.setText(cursor.getString(cursor.getColumnIndex(AppConstants.CNAME)));
	        basic2.setText(getStatus(cursor.getString(cursor.getColumnIndex(AppConstants.STATUS))));
	        
	        if(info1!=null)
	        {
	        	String before = cursor.getString(cursor.getColumnIndex(AppConstants.VALUE)) + " ";
				String location = cursor.getString(cursor.getColumnIndex(AppConstants.LOCATION));
	        	String type = cursor.getString(cursor.getColumnIndex(AppConstants.TYPE));
	        	if(type.equals(AppConstants.TIME))
	        		before+= " "+c.getString(R.string.minutes);
				else	
					before+= " "+c.getString(R.string.meters);
	        	
	        	info1.setText(location);
	        	info2.setText(before);
	        	
	        	if (Locale.getDefault().getLanguage().equals("ar"))
	        	{
	        		info3.setText(cursor.getString(cursor.getColumnIndex(AppConstants.START))
	        				+": "+c.getString(R.string.starts));
		        	info4.setText(cursor.getString(cursor.getColumnIndex(AppConstants.END))
		        			+": "+c.getString(R.string.ends));
	        	}
	        	else
	        	{
	        		info3.setText(c.getString(R.string.starts)+": "+
		        			cursor.getString(cursor.getColumnIndex(AppConstants.START)));
		        	info4.setText(c.getString(R.string.ends)+": "+
		        			cursor.getString(cursor.getColumnIndex(AppConstants.END)));
	        	}
	        	
	        	info3.setVisibility(View.VISIBLE);
	        	info4.setVisibility(View.VISIBLE);
	        }
		}

		/**
		 * Initializes the view for the first time
		 * @author Aya Kamel
		 */
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(context);
			View v = inflater.inflate(R.layout.list_item_updated, parent, false);
			return v;
		}

		/**
		 * Updates the views everytime when anything changes
		 * @author Aya Kamel
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = super.getView(position, convertView, parent);
			if(v!=null)
			{				
				//Maintain visibility
				maintainVisibility(position);
				
				//Maintain selected/deselected
				long id = getItemId(position);
				
				if(id == cur_sel)
					v.setBackgroundColor(Color.DKGRAY);
				else
					v.setBackgroundColor(Color.BLACK);
			}
			return v;
		}
		
		/**
		 * Is called from getView to keep expanded stubs expanded and shrinked
		 * stubs shrinked
		 * @param position
		 * 		The index of the item being maintained
		 * @author Aya Kamel
		 */
		private void maintainVisibility(int position)
		{
			//Maintain expand/shrink
			if(getExpanded(position))
			{
				info1.setVisibility(View.VISIBLE);
				info2.setVisibility(View.VISIBLE);
				if(extended)
				{
					info3.setVisibility(View.VISIBLE);
					info4.setVisibility(View.VISIBLE);
				}
			}
			else
			{
				info1.setVisibility(View.GONE);
				info2.setVisibility(View.GONE);
				if(extended)
				{
					info3.setVisibility(View.GONE);
					info4.setVisibility(View.GONE);
				}
			}
		}

		/**
		 * Removes a specific item from the list of stati to avoid
		 * having redundant data
		 * @param pos
		 * 		The index of the item in the list
		 * @author Aya Kamel
		 */
		public void remove(int pos) 
		{
			stat.remove(pos);
		}

	}
